package com.huel.taobaostore;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.TextView;

import java.util.Timer;
import java.util.TimerTask;

public class Welcome extends Activity {
    TextView spalshTextview;
    private int recLen = 5;//跳过倒计时提示5秒
    private Timer timer = new Timer();
    private Handler handler;
    private Runnable runnable;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.welcome);
        spalshTextview=findViewById(R.id.spalsh_textview);
        spalshTextview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jump();
            }
        });
        initData();
        timer.schedule(task, 1000, 1000);//等待时间一秒，停顿时间一秒
    }


    TimerTask task = new TimerTask() {
        @Override
        public void run() {
            runOnUiThread(new Runnable() { // UI thread
                @Override
                public void run() {
                    recLen--;
                    spalshTextview.setText("跳过 " + recLen);

                    if (recLen < 0) {
                        timer.cancel();
                        spalshTextview.setVisibility(View.GONE);//倒计时到0隐藏字体

                    }
                    if (recLen < 2) {
                        spalshTextview.setEnabled(false);
                    }
                }
            });
        }
    };

    protected void initData() {
        /**
         * 正常情况下不点击跳过
         */
        handler = new Handler();
        handler.postDelayed(runnable = new Runnable() {
            @Override
            public void run() {
                jump();

            }
        }, 3000);//延迟5S后发送handler信息
    }
    private void jump() {
        handler.removeCallbacks(runnable);
                startActivity(new Intent(Welcome.this, MainActivity.class));
            finish();

    }
}
